<template>
    <div>
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :immediate-check="false"
          offset="50"
          @load="onLoad"
        >
          <ArticleItem v-for="obj in list"
          :artObj="obj" :key="obj.art_id"
          @disLikeEV="disLikeFn"
          @reportEV="reportFn"
          @click.native="itemClickFn(obj.art_id)"
          ></ArticleItem>
        </van-list>
      </van-pull-refresh>
    </div>
</template>
<script>
import ArticleItem from '../../../components/ArticleItem.vue'
import { getAllArticleListAPI, dislikeArticleAPI, reportArticleAPI } from '@/api'
import Notify from '@/ui/Notify.js'
export default {
  props: {
    // list: Array
    channelId: Number
  },
  components: {
    ArticleItem
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      theTime: (new Date()).getTime(),
      isLoading: false
    }
  },
  methods: {
    async getArticleListFn () {
      const res = await getAllArticleListAPI(
        {
          channel_id: this.channelId,
          timestamp: this.theTime
        })
      // console.log(res)

      this.list = [...this.list, ...res.data.data.results]
      this.theTime = res.data.data.pre_timestamp
      this.loading = false// 如果不关闭，底部一直是加载状态
      if (res.data.data.pre_timestamp === null) {
        // 本次回来的数据是最后，没有下端数据了
        this.finished = true
      }
      // 顶部加载状态改为false
      this.isLoading = false
    },
    async onLoad () {
      if (this.list.length === 0) {
        this.loading = false
        return
      }
      this.getArticleListFn()
    },
    // 顶部刷新
    async onRefresh () {
      this.list = []
      this.theTime = new Date().getTime()
      this.getArticleListFn()
    },
    async disLikeFn (artId) {
      await dislikeArticleAPI({ artId })
      // console.log(res)
      Notify({ type: 'success', message: '反馈成功' })
    },
    async reportFn (artId, type) {
      await reportArticleAPI({ artId, type })
      // console.log(res)
      Notify({ type: 'success', message: '举报成功' })
    },
    // 文章单元格点击事件
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  },

  created () {
    this.getArticleListFn()
  }
}
</script>
<style lang='less' scoped>
</style>
